// 功能二：实现购物车加或者减的功能
window.onload = function(){
    // 获取所有列表前面的checkbox
    let checkBoxs = document.querySelectorAll(".item-check-btn");
    checkBoxs.forEach(checkEle =>{
        checkEle.isCheck = false;
        checkEle.onclick = function(){
            if( !checkEle.isCheck){
                // checkEle.className += ' check';
                checkEle.classList.add("check");
            }else{
                // checkEle.className = 'item-check-btn'
                checkEle.classList.remove("check");
            }
           this.isCheck = !this.isCheck;
        }
    })



    // 实现加和减的功能  ，通过事件委托
    let eles = document.querySelectorAll(".quentity");
    eles.forEach(ele=>{
        ele.onclick = function(e){
            var e = e || window.event;
            // console.log(e.target);
            // 把点击+ 和 -的事假委托给 外层容器quentity
            if(e.target.innerText==='-'){
                // console.log("点击了-号");
                let num = e.target.nextElementSibling.value;
                // console.log(num);
                if(num>1){
                    e.target.nextElementSibling.value = parseInt(e.target.nextElementSibling.value)-1;
                }
            }else if(e.target.innerText==='+'){
                // console.log("点击了+号");
                let num = e.target.previousElementSibling.value;
                // console.log(num)
                e.target.previousElementSibling.value = parseInt(e.target.previousElementSibling.value)+1;
            }

            // 计算每一条的总金额
            // 获取 单价 
            let priceEle = ele.parentNode.parentNode.parentNode.previousElementSibling.querySelector(".item-price");
            let price = parseInt( priceEle.innerText.substr(1));
            let num = ele.querySelector("input").value;
            // console.log(num);
            let total = price*num;
            let totalEle = ele.parentNode.parentNode.parentNode.nextElementSibling.querySelector(".item-price-total");
            // console.log(totalEle)
            totalEle.innerText = total;
        }
    })

}